using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SharpCore.Data;
using SharpCore.Utilities;

namespace Dating.DA
{
	public class MessageIdeaDA
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors
		public MessageIdeaDA()
		{
		}

		public MessageIdeaDA(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the MessageIdea table.
		/// </summary>
		public  int Insert(MessageIdea messageIdea)
		{
			ValidationUtility.ValidateArgument("messageIdea", messageIdea);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", messageIdea.TenantID),
				new SqlParameter("@Message", messageIdea.Message)
			};

			messageIdea.IdeaID = Convert.ToInt32(SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "MessageIdeaInsert", parameters));
			return messageIdea.IdeaID;
		}

		/// <summary>
		/// Updates a record in the MessageIdea table.
		/// </summary>
		public  int Update(MessageIdea messageIdea)
		{
			ValidationUtility.ValidateArgument("messageIdea", messageIdea);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdeaID", messageIdea.IdeaID),
				new SqlParameter("@TenantID", messageIdea.TenantID),
				new SqlParameter("@Message", messageIdea.Message)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MessageIdeaUpdate", parameters));
		}

		/// <summary>
		/// Deletes a record from the MessageIdea table by its primary key.
		/// </summary>
		public  int Delete(int ideaID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdeaID", ideaID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MessageIdeaDelete", parameters));
		}

		/// <summary>
		/// Deletes a record from the MessageIdea table by a foreign key.
		/// </summary>
		public  int DeleteAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MessageIdeaDeleteAllByTenantID", parameters));
		}

		/// <summary>
		/// Selects a single record from the MessageIdea table.
		/// </summary>
		public  MessageIdea Select(int ideaID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdeaID", ideaID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MessageIdeaSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MakeMessageIdea(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the MessageIdea table.
		/// </summary>
		public  List<MessageIdea> SelectAll(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)
			};
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MessageIdeaSelectAll", parameters))
			{
				List<MessageIdea> messageIdeaList = new List<MessageIdea>();
				while (dataReader.Read())
				{
					MessageIdea messageIdea = MakeMessageIdea(dataReader);
					messageIdeaList.Add(messageIdea);
				}

				return messageIdeaList;
			}
		}

		/// <summary>
		/// Selects all records from the MessageIdea table by a foreign key.
		/// </summary>
		public  List<MessageIdea> SelectAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MessageIdeaSelectAllByTenantID", parameters))
			{
				List<MessageIdea> messageIdeaList = new List<MessageIdea>();
				while (dataReader.Read())
				{
					MessageIdea messageIdea = MakeMessageIdea(dataReader);
					messageIdeaList.Add(messageIdea);
				}

				return messageIdeaList;
			}
		}

		/// <summary>
		/// Creates a new instance of the MessageIdea class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected  MessageIdea MakeMessageIdea(SqlDataReader dataReader)
		{
			MessageIdea messageIdea = new MessageIdea();
			messageIdea.IdeaID = SqlClientUtility.GetInt32(dataReader, "IdeaID", 0);
			messageIdea.TenantID = SqlClientUtility.GetInt32(dataReader, "TenantID", 0);
			messageIdea.Message = SqlClientUtility.GetString(dataReader, "Message", String.Empty);

			return messageIdea;
		}

		#endregion
	}
}
